<template>
    <div class="app-container">
        <!-- 添加或修改单据主对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <b>*名称:</b>
                <template>
                    <el-input v-model="material.name" placeholder="请输入名称">
                    </el-input>
                </template>
                <b>规格:</b>
                <template>
                    <el-input v-model="material.standard" placeholder="请输入规格">
                    </el-input>
                </template>
                <b>型号:</b>
                <template>
                    <el-input v-model="material.model" placeholder="请输入型号">
                    </el-input>
                </template><br>
                <b>*单位:</b>
                <template>
                    <el-input v-model="material.unit" placeholder="请输入单位">
                    </el-input>
                </template>
                <b>颜色:</b>
                <template>
                    <el-input v-model="material.color" placeholder="请输入颜色">
                    </el-input>
                </template>
                <b>基础重量:</b><br>
                <template>
                    <el-input-number v-model="material.weight" :precision="2" :step="0.1" :max="10"></el-input-number>
                </template>
                <b>保质期:</b>
                <el-input-number v-model="material.expiryNum" :precision="2" :step="0.1" :max="10"></el-input-number>
                <b>类别:</b>
                <el-select v-model="material.categoryId" placeholder="" clearable style="width: 240px">
                    <el-option v-for="dict in categoryList" :key="dict.name" :label="dict.name" :value="dict.id" />
                </el-select>
                <b>序列号:</b>
                <el-select v-model="material.enableSerialNumber" placeholder="" clearable style="width: 240px">
                    <el-option v-for="dict in BatchN" :key="dict.label" :label="dict.label" :value="dict.value" />
                </el-select>
                <b>批号:</b>
                <el-select v-model="material.enableBatchNumber" placeholder="" clearable style="width: 240px">
                    <el-option v-for="dict in BatchN" :key="dict.label" :label="dict.label" :value="dict.value" />
                </el-select>
                <el-divider content-position="center" />
                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" icon="el-icon-plus" size="mini"
                            @click="handleAddJshDepotItem">插入行</el-button>
                    </el-col>
                </el-row>
                <el-table ref="jshMaterialExtend" :data="jshMaterialExtendList" :row-class-name="rowJshDepotItemIndex"
                    @selection-change="handleJshDepotItemSelectionChange">
                    <el-table-column type="selection" width="50" align="center" />
                    <el-table-column label="条码" prop="barCode " width="150">
                        <template slot-scope="scope">
                            <el-input v-model="scope.row.barCode" placeholder="请输入条码">
                            </el-input>
                        </template>
                    </el-table-column>
                    <el-table-column label="单位" prop="commodityUnit" width="150">
                        <template slot-scope="scope">
                            <el-input v-model="scope.row.commodityUnit" placeholder="请输入商品单价" />
                        </template>
                    </el-table-column>
                    <el-table-column label="采购价" prop="purchaseDecimal" width="150">
                        <template slot-scope="scope">
                            <el-input v-model="scope.row.purchaseDecimal" placeholder="请输入采购价格" />
                        </template>
                    </el-table-column>
                    <el-table-column label="零售价" prop="commodityDecimal" width="150">
                        <template slot-scope="scope">
                            <el-input v-model="scope.row.commodityDecimal" placeholder="请输入商品零售价" />
                        </template>
                    </el-table-column>
                    <el-table-column label="销售价" prop="wholesaleDecimal" width="150">
                        <template slot-scope="scope">
                            <el-input v-model="scope.row.wholesaleDecimal" placeholder="请输入销售价" />
                        </template>
                    </el-table-column>
                    <el-table-column label="最低售价" prop="wholesaleDecimal" width="150">
                        <template slot-scope="scope">
                            <el-input v-model="scope.row.lowDecimal" placeholder="请输入最低售价" />
                        </template>
                    </el-table-column>
                </el-table>
                <b>备注:</b><el-input v-model="material.remark" type="text" />
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import depot from '@/api/depot/depot'
import depotApi from '@/api/depot/depot'
import { getDepotHeadAndDetail, updateDepotHeadAndDetail, addDepotHeadAndDetail, getMaterialListByCode, getcategoryList, getDepotMaterialList, findDepot, buildNumber, deleteBatch, upStatus, listUser, getDepotNameList, listSupplier, listHead, getHead, delHead, addHead, updateHead } from '@/api/depot/otherIn'
import { runInThisContext } from 'vm'

export default {
    name: 'Head',
    data() {
        return {
            d: '',
            rows: [],
            BatchN: [{
                value: 1,
                label: '是'
            },
            {
                value: 0,
                label: '否'
            }],


        }
    },
    created() {
        getcategoryList().then(res => {
            console.log(res.data)
            this.categoryList = res.data
        })
    },

    methods: {
        /** 新增按钮操作 */
        handleAdd() {
            this.open = true
            this.info.jshDepotItemList = []
            this.info.remark = ''
            buildNumber(this.queryParams.type, this.queryParams.subType).then(res => {
                this.info.number = res.defaultNumber
            })
            this.info.createTime = new Date()
            this.type = 0
            handleAddJshDepotItem()
        },
        /** 修改按钮操作 */
        handleUpdate(id) {
            getDepotHeadAndDetail(id).then(res => {
                this.info = res.info
                this.jshDepotItemList = res.jshDepotItemList
                this.open = true
                this.type = 1
            })
        },
        /** 新增 */
        submitForm() {
            var aa = true
            for (var i = 0; i < this.jshDepotItemList.length; i++) {
                if (this.jshDepotItemList[i].depotId == null || this.jshDepotItemList[i].depotId == '') {
                    aa = false
                }
            }
            if (aa) {
                this.info.jshDepotItemList = this.jshDepotItemList
                console.log(this.jshDepotItemList)
                console.log(this.info.jshDepotItemList)
                if (this.type == 0) {
                    addDepotHeadAndDetail(this.info).then(res => {
                        if (res.code == '200') {
                            this.cancel(),
                                this.getList()
                            location.reload()
                        }
                    })
                } else {
                    updateDepotHeadAndDetail(this.info).then(res => {
                        this.cancel(),
                            this.getList()
                    })
                }
            } else {
                alert('请选择仓库')
            }
        },
        /** 单据子添加按钮操作 */
        handleAddJshDepotItem() {
            const obj = {}
            obj.materialId = ''
            obj.materialExtendId = ''
            obj.materialUnit = ''
            obj.sku = ''
            obj.operNumber = 1
            obj.basicNumber = ''
            obj.unitPrice = ''
            obj.purchaseUnitPrice = ''
            obj.taxUnitPrice = ''
            obj.allPrice = ''
            obj.remark = ''
            obj.depotId = ''
            obj.anotherDepotId = ''
            obj.taxRate = ''
            obj.taxMoney = ''
            obj.taxLastMoney = ''
            obj.materialType = ''
            obj.snList = ''
            obj.batchNumber = ''
            obj.expirationDate = ''
            obj.linkId = ''
            obj.tenantId = ''
            obj.mbarCode = ''
            this.jshDepotItemList.push(obj)
        },
        /** 复选框选中数据 */
        handleJshDepotItemSelectionChange(selection) {
            this.checkedJshDepotItem = selection.map(item => item.index)
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('system/head/export', {
                ...this.queryParams
            }, `head_${new Date().getTime()}.xlsx`)
        }
    }
}
</script>
